#include "MatchProcParamCollectRequestPanel.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include "../../../../models/MatchProcParamCollectData.h"
#include "../../../../models/CommonData.h"
#include "../../../../controller/SignalBus.h"

MatchProcParamCollectRequestPanel::MatchProcParamCollectRequestPanel(QWidget *parent)
    : QWidget{parent}
{
    paramListLayout = nullptr;

    setupUi();
}

MatchProcParamCollectRequestPanel::~MatchProcParamCollectRequestPanel()
{
    delete paramListLayout;
    paramListLayout = nullptr;
}

void MatchProcParamCollectRequestPanel::initWidgets()
{
    const auto headLayout = new QGridLayout();
    const auto lbWorkStep = new QLabel(tr("工步"));
    const auto txtWorkStep = new QLineEdit();
    const auto lbLocation = new QLabel(tr("工位"));
    const auto txtLocation = new QLineEdit();
    const auto lbPcu = new QLabel(tr("产品SN"));
    const auto txtPcu = new QLineEdit();
    const auto btnAddParam = new QPushButton(tr("添加请求参数"));
    const auto btnExecute = new QPushButton(tr("执行请求"));

    headLayout->addWidget(lbWorkStep,0,0,1,1);
    headLayout->addWidget(txtWorkStep,0,1,1,1);
    headLayout->addWidget(lbLocation,1,0,1,1);
    headLayout->addWidget(txtLocation,1,1,1,1);
    headLayout->addWidget(lbPcu,2,0,1,1);
    headLayout->addWidget(txtPcu,2,1,1,1);

    layout = new QGridLayout();
    btnExecute->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
    headLayout->addWidget(btnAddParam,3,0,1,3);
    headLayout->addWidget(btnExecute,0,2,3,1);
    layout->addLayout(headLayout,0,0,1,1);

    connect(btnAddParam,&QPushButton::clicked,this,&MatchProcParamCollectRequestPanel::addParamPanel);
    connect(btnExecute,&QPushButton::clicked,this,&MatchProcParamCollectRequestPanel::execute);

    valueList.insert("pcu",txtPcu);
    valueList.insert("workStep",txtWorkStep);
    valueList.insert("location",txtLocation);

    setLayout(layout);
}

void MatchProcParamCollectRequestPanel::setupUi()
{
    initWidgets();
}

QWidget *MatchProcParamCollectRequestPanel::buildParamInfo(QHash<QString,QLineEdit*>* widgetList)
{
    const auto widget = new QWidget();

    const auto lbKey = new QLabel(tr("参数编号"));
    const auto txtKey = new QLineEdit();
    const auto lbValue = new QLabel(tr("参数值"));
    const auto txtValue = new QLineEdit();
    const auto lbSeq = new QLabel(tr("工步"));
    const auto txtSeq = new QLineEdit();
    const auto lbLocation = new QLabel(tr("工位"));
    const auto txtLocation = new QLineEdit();
    const auto layout = new QGridLayout();


    layout->addWidget(lbKey,0,0,1,1);
    layout->addWidget(txtKey,0,1,1,1);
    layout->addWidget(lbValue,1,0,1,1);
    layout->addWidget(txtValue,1,1,1,1);
    layout->addWidget(lbSeq,2,0,1,1);
    layout->addWidget(txtSeq,2,1,1,1);
    layout->addWidget(lbLocation,3,0,1,1);
    layout->addWidget(txtLocation,3,1,1,1);
    widget->setLayout(layout);

    widgetList->insert("k",txtValue);
    widgetList->insert("v",txtKey);
    widgetList->insert("seq",txtSeq);
    widgetList->insert("location",txtLocation);

    return widget;
}

void MatchProcParamCollectRequestPanel::execute()
{
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS");
    QString transIdStr = QString("%1%2").arg(dateTime).arg(randomNumber);

    std::string pcu = valueList["pcu"]->text().toStdString();
    std::string workStep = valueList["workStep"]->text().toStdString();
    std::string location = valueList["location"]->text().toStdString();
    std::list<Params> params;

    for(const auto& item:std::as_const(paramCollection))
    {
        params.push_back({
            item["k"]->text().toStdString(),
            item["v"]->text().toStdString(),
            item["seq"]->text().toStdString(),
            item["location"]->text().toStdString()
        });
    }

    BaseData<MatchProcParamCollectData>::execute(&MatchProcParamCollectData::setPcu,pcu);
    BaseData<MatchProcParamCollectData>::execute(&MatchProcParamCollectData::setParamList,params);
    BaseData<MatchProcParamCollectData>::execute(&MatchProcParamCollectData::setWorkStep,workStep);
    BaseData<MatchProcParamCollectData>::execute(&MatchProcParamCollectData::setLocation,location);
    BaseData<CommonData>::execute(&CommonData::setEventTime,dateTime.toStdString());
    BaseData<CommonData>::execute(&CommonData::setTransId,transIdStr.toStdString());

    emit SignalBus::instance()->sendMatchProcParamCollect();
}

void MatchProcParamCollectRequestPanel::addParamPanel()
{

    if(paramListLayout == nullptr)
        paramListLayout = new QGridLayout();
    else
    {
        if(!paramWidgetsList.empty())
        {
            for(auto item:std::as_const(paramWidgetsList))
            {
                paramListLayout->removeWidget(item);
                item->deleteLater();
                item = nullptr;
            }
            paramWidgetsList.clear();
        }
        paramListLayout->deleteLater();
        paramListLayout = nullptr;
        paramListLayout = new QGridLayout();

    }

    int row = 0;
    int col = 0;
    int count = paramCollection.size();

    for(int cursor=0;cursor<=count;cursor++)
    {
        const auto widget = buildParamInfo(&paramInfoList);
        paramWidgetsList.append(widget);
        paramListLayout->addWidget(widget,row,col,1,1);
        col++;
        if(col%3==0)
        {
            row++;
            col=0;
        }
    }

    layout->addLayout(paramListLayout,layout->rowCount(),0,1,1);
    paramCollection.append(paramInfoList);
}
